Emberveil sourcegame/2006Scape Server/src/main/java/com/rs2/world/GlobalDropsHandler.java
package com.rs2.world;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rs2.event.CycleEvent;
import com.rs2.event.CycleEventContainer;
import com.rs2.event.CycleEventHandler;
import com.rs2.game.players.Client;
import com.rs2.game.players.Player;
import com.rs2.game.players.PlayerHandler;
import com.rs2.util.GlobalDropData;
import com.rs2.util.Misc;
import com.rs2.util.ShopData;
import org.apollo.cache.def.ItemDefinition;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Handles global drops which respawn after set amount of time when taken
*
* @author Stuart <RogueX>
*/
public class GlobalDropsHandler {
/**
* time in seconds it takes for the item to respawn
*/
private static final int TIME_TO_RESPAWN = 20;
/**
* holds all the objects
*/
private static final List<GlobalDrop> globalDrops = new ArrayList<>();
private static final Set<GlobalDrop> spawnedDrops = new HashSet<>();
/**
* loads the items
*/
private static boolean initialized;
public static void initialize() {
if(initialized)return;
try(FileReader input=new FileReader("./data/cfg/globaldrops.json")) {
GlobalDropData[] definitions=new Gson().fromJson(input,GlobalDropData[].class);
for(GlobalDropData data:definitions)globalDrops.add(new GlobalDrop(data.getId(),data.getAmount(),data.getItemX(),data.getItemY(),data.getHeight()));
initialized=true;
} catch(Exception failure) {throw new IllegalStateException("Unable to load global drops",failure);}
System.out.println("Loaded "+globalDrops.size()+" global drops.");
CycleEventHandler.getSingleton().addEvent(GlobalDropsHandler.class,new CycleEvent(){
public void execute(CycleEventContainer container){respawn(System.currentTimeMillis());}
public void stop(){}
},1);
}
static void respawn(long now) {
for(GlobalDrop drop:globalDrops)if(drop.isTaken() && now-drop.getTakenAt()>=TIME_TO_RESPAWN*1000L) {
drop.setTaken(false);drop.setSpawned(true);spawnedDrops.add(drop);
for(Player player:PlayerHandler.players)if(player!=null && player.isActive && !player.disconnected && player.heightLevel==drop.getHeight() && player.distanceToPoint(drop.getX(),drop.getY())<=60)
player.getPacketSender().createGroundItem(drop.getId(),drop.getX(),drop.getY(),drop.getAmount(),drop.getHeight());
}
}
public static void writeGlobalDropsDump() {
String Data;
BufferedReader Checker;
JSONArray array = new JSONArray();
try {
Checker = new BufferedReader(new FileReader("./data/cfg/globaldrops.txt"));
while ((Data = Checker.readLine()) != null) {
if (Data.startsWith("#")) {
continue;
}
String[] args = Data.split(":");
JSONObject object = new JSONObject();
object.put("id", Integer.parseInt(args[0]));
object.put("amount", args[1].replace("_", " "));
object.put("itemX", Integer.parseInt(args[2]));
object.put("itemY", Integer.parseInt(args[3]));
if (args.length == 5) {
object.put("height", Integer.parseInt(args[4]));
}
array.put(object);
}
Checker.close();
FileWriter fileWriter = new FileWriter("globaldrops-dump.json");
fileWriter.write(array.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* See if a drop exists at the given place
*
* @param a item id
* @param b x cord
* @param c y cord
*
* @return return the statement
*/
private static GlobalDrop itemExists(int a, int b, int c, int height) {
for (GlobalDrop drop : globalDrops) {
if (drop.getId() == a && drop.getX() == b && drop.getY() == c && drop.getHeight()==height) {
return drop;
}
}
return null;
}
public static boolean itemExists(int itemID, int itemX, int itemY, boolean yes) {
for (GlobalDrop drop : spawnedDrops) {
if (drop.getId() == itemID && drop.getX() == itemX && drop.getY() == itemY) {
return true;
}
}
return false;
}
/**
* Pick up an item at the given location
*
* @param player the Player
* @param itemID item id
* @param itemX cord x
* @param itemY cord y
*/
public static void pickup(Player player, int itemID, int itemX, int itemY) {
if (!com.rs2.game.content.quests.impl.ClockTower.canPickup(player,itemID,itemX,itemY)) return;
GlobalDrop drop = itemExists(itemID, itemX, itemY,player.heightLevel);
if (drop == null) {
return;
}
if (drop.isTaken()) {
return;
}
if (player.getItemAssistant().freeSlots() < 1) {
if (!(player.getItemAssistant().playerHasItem(player.pItemId) && ItemDefinition.lookup(player.pItemId).isStackable())) {
player.getPacketSender().sendMessage("Not enough space in your inventory.");
return;
}
}
if(!player.getItemAssistant().addItem(drop.getId(), drop.getAmount()))return;
drop.setTakenAt(System.currentTimeMillis());
drop.setTaken(true);
for (Player playerLoop : PlayerHandler.players) {
Client cl = (Client) playerLoop;
if (cl != null && cl.isActive && !cl.disconnected && cl.heightLevel==drop.getHeight()) {
cl.getPacketSender().removeGroundItem(drop.getId(), drop.getX(), drop.getY(), drop.getAmount());
spawnedDrops.remove(drop);
}
}
}
/**
* Loads all the items when a player changes region
*
* @param player the Player
*/
public static void load(Client player) {
initialize();
for(GlobalDrop drop:globalDrops)if(!drop.isTaken() && player.heightLevel==drop.getHeight() && player.distanceToPoint(drop.getX(),drop.getY())<=60) {
player.getPacketSender().createGroundItem(drop.getId(),drop.getX(),drop.getY(),drop.getAmount(),drop.getHeight());
spawnedDrops.add(drop);drop.setSpawned(true);
}
}
public static void reset(Player player) {
for(GlobalDrop drop:globalDrops)if(player.heightLevel==drop.getHeight() && player.distanceToPoint(drop.getX(),drop.getY())<=60)
player.getPacketSender().removeGroundItem(drop.getId(),drop.getX(),drop.getY(),drop.getAmount());
load((Client)player);
}
/**
* Holds each drops data
*
* @author Stuart
*/
static class GlobalDrop {
/**
* cord x
*/
int itemX;
/**
* cord y
*/
int itemY;
private int height;
/**
* item id
*/
int id;
/**
* item amount
*/
int amount;
/**
* has the item been taken
*/
boolean taken = false;
private boolean spawned = false;
/**
* Time it was taken at
*/
long takenAt;
/**
* Sets the drop arguments
*
* @param id item id
* @param amount item amount
* @param itemX cord x
* @param itemY cord y
*/
public GlobalDrop(int id, int amount, int itemX, int itemY) {
this.id = id;
this.amount = amount;
this.itemX = itemX;
this.itemY = itemY;
}
public GlobalDrop(int id, int amount, int itemX, int itemY, int height) {
this.id = id;
this.amount = amount;
this.itemX = itemX;
this.itemY = itemY;
this.height = height;
}
/**
* get cord x
*
* @return return the statement
*/
public int getX() {
return itemX;
}
/**
* get cord x
*
* @return return the statement
*/
public int getY() {
return itemY;
}
/**
* get the item id
*
* @return return the statement
*/
public int getId() {
return id;
}
/**
* get the item amount
*
* @return return the statement
*/
public int getAmount() {
return amount;
}
/**
* has the drop already been taken?
*
* @return return the statement
*/
public boolean isTaken() {
return taken;
}
/**
* set if or not the drop has been taken
*
* @param a true yes false no
*/
public void setTaken(boolean a) {
taken = a;
}
/**
* set the time it was picked up
*
* @param a the a
*/
public void setTakenAt(long a) {
takenAt = a;
}
/**
* get the time it was taken at
*
* @return return the statement
*/
public long getTakenAt() {
return takenAt;
}
public boolean isSpawned() {
return spawned;
}
public void setSpawned(boolean spawned) {
this.spawned = spawned;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
}